package cn.tedu.csmall.commons.restful;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.List;

//通用支持分页支持的结果对象类型
@Data
public class JsonPage<T> implements Serializable {
    //按照实际需求定义这个类中的属性
    @ApiModelProperty(value = "当前页码",name = "pageNum")
    private Integer pageNum;
    @ApiModelProperty(value = "每页显示条数",name = "pageSize")
    private Integer pageSize;
    @ApiModelProperty(value = "总条数",name = "totalCount")
    private Long totalCount;
    @ApiModelProperty(value = "总页数",name = "totalPages")
    private Integer totalPages;
    //声明属性来承载查询到的结果
    @ApiModelProperty(value = "分页数据",name = "list")
    private List<T> list;

    //下面编写其他框架的分页结果转换成当前类对象的方法
    //SpringData,Elasticsearch或PageHelper等具有分页功能的框架,均有类似PageInfo的对象
    //我们可以分别编写方法,将它们转换成JsonPage对象,先只编写PageHelper的转换
    public static <T> JsonPage<T> resultPage(PageInfo<T> pageInfo){
        //下面开始将pageInfo转为JsonPage
        JsonPage<T> result = new JsonPage<>();
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotalCount(pageInfo.getTotal());
        result.setTotalPages(pageInfo.getPages());
        result.setList(pageInfo.getList());
        return result;
    }


}










